<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="菜单分类名" prop="serverName">
        <el-input
          v-model="dataForm.serverName"
          placeholder="菜单分类名"
        ></el-input>
      </el-form-item>
      <el-form-item label="菜单的ioc图标" prop="icon">
        <single-upload  v-model="dataForm.icon"></single-upload>
      </el-form-item>
      <el-form-item label="状态" prop="isShow">
        <el-switch
          v-model="dataForm.isShow"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from "@/components/upload/singleUpload";
export default {
    components: { singleUpload } /* 注册进来 */,
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        serverName: "",
        icon: "",
        isShow: "",
        page: "",
      },
      dataRule: {
        serverName: [
          { required: true, message: "菜单分类名不能为空", trigger: "blur" },
        ],
        icon: [
          { required: true, message: "菜单的ioc图标不能为空", trigger: "blur" },
        ],
        isShow: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        page: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/help/menu/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.serverName = data.menu.serverName;
              this.dataForm.icon = data.menu.icon;
              this.dataForm.isShow = data.menu.isShow;
              this.dataForm.page = data.menu.page;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/help/menu/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              serverName: this.dataForm.serverName,
              icon: this.dataForm.icon,
              isShow: this.dataForm.isShow,
              page: this.dataForm.page,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
